﻿using Futaba.SBus.Telemetry;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Futaba.SBus.SampleApplication.TelemetryLiveView
{
	class ViewModel : NotifyPropertyChangedBase
	{

		public ViewModel()
		{
			using (var stream = GetType().Assembly.GetManifestResourceStream(typeof(MainWindow).Namespace + ".example_model_configuration.xml"))
			{
				Slots = Configuration.ModelConfigurationFactory.DefaultInstance.Load(stream)
					.Slots.Select(s => new SlotWithHistory(s)).ToArray();
			}
		}

		private SlotWithHistory[] _internalSlots;
		public SlotWithHistory[] Slots
		{
			get { return _internalSlots; }
			set
			{
				if (Equals(_internalSlots, value))
					return;
				_internalSlots = value;
				RaisePropertyChanged(); //RaisePropertyChanged("Slots");
				ResetSlotsView();
			}
		}

		ICollectionView _slotsView;
		public ICollectionView SlotsView
		{
			get
			{
				if (_slotsView == null)
				{
					var s = System.Windows.Data.CollectionViewSource.GetDefaultView(Slots);
					using (s.DeferRefresh())
					{
						s.SortDescriptions.Add(new SortDescription("SortOrder", ListSortDirection.Ascending));
						//s.GroupDescriptions.Add(new PropertyGroupDescription("ChannelId"));
					}
					_slotsView = s;
				}
				return _slotsView;
			}
		}

		protected internal void ResetSlotsView()
		{
			_slotsView = null;
			RaisePropertyChanged("SlotsView");
		}		

	}
}
